package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    private final SetmealService setmealService;

    /**
     * 套餐分页查询
     * @param categoryId
     * @param name
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/page")
    public Result<Map<String,Object>> findAllPage(
            @RequestParam (value = "categoryId", required = false) Integer categoryId,
            @RequestParam (value = "categoryId", required = false) String name,
            @RequestParam Integer page,
            @RequestParam Integer pageSize,
            @RequestParam (value = "categoryId", required = false) Integer status
    ){
        Map<String,Object> map = setmealService.findAllPage(categoryId, name, page, pageSize, status);
        return Result.success(map);
    }

    /**
     * 添加套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    public Result<Object> addSetmeal(@RequestBody SetmealDTO setmealDTO){
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmealDTO, setmealVO);
        setmealService.addSetmeal(setmealVO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> findById(@PathVariable Integer id){
        SetmealVO setmealVO = setmealService.finById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    public Result<Object> updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<Object> enableOrDisabled
    (@PathVariable Integer status,
     @RequestParam Integer id){
        Setmeal setmealById = setmealService.getById(id);
        setmealById.setStatus(status);
        setmealService.updateById(setmealById);
        return Result.success();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<Object> deleteByIds(@RequestParam List<Integer> ids){
        setmealService.removeByIds(ids);
        return Result.success();
    }
}
